<template>
  <div class="withdrawal">
    <div>
      <van-nav-bar
        title="Withdrawal"
        left-arrow
        @click-left="$router.back()"
        right-text="Record"
        @click-right="$router.push('/withdrawalrecord')"
      />
    </div>
    <div v-if="iscrads">
      <div class="with-card" v-for="cd in cardData" :key="cd.id">
        <div class="icon">
          <i class="iconfont icon-Withdrawal2"></i>
        </div>
        <div class="card-text">
          <p>bank card</p>
          <p class="bank-number">{{ cd.number }}</p>
        </div>
      </div>
    </div>
    <div class="with-card" v-else>
      <div class="icon">
        <i class="iconfont icon-Withdrawal2"></i>
      </div>
      <div class="card-text">No bank card bound</div>
      <div class="card-add">
        <button @click="$router.push('/bankcard')">Add bank card</button>
      </div>
    </div>

    <div class="withdrawal-content">
      <p>Amount:</p>
      <div class="pt-num">
        <span>₹</span>
        <input type="number" v-model="amountValue" maxlength="6" />
      </div>
      <div class="bd-line"></div>
      <div class="fee">
        <van-icon name="warning" color="#FF4F02" /> <span>5% Handling fee</span>
      </div>
      <div class="with-btn">
        <button @click="withdrawalHandle">Withdrawal</button>
      </div>
    </div>
  </div>
</template>

<script>
import { getCookie, Money, getStorage } from "@/utils/common";
import { Toast } from "vant";
export default {
  data() {
    return {
      amountValue: "",
      iscrads: false,
      cardData: [],
    };
  },
  mounted() {
    this.$store.dispatch("cards").then((res) => {
      this.cardData = res;
      if (res.length !== 0) {
        this.iscrads = true;
      } else {
        this.iscrads = false;
      }
    });
  },
  methods: {
    paymengHandle() {
      if (this.amountValue) {
        this.loading = true;
        this.$store
          .dispatch("withdrawal", { amount: Money(this.amountValue) })
          .then((res) => {
            if (res.code == 0) {
              this.$toast(
                "The withdrawal application is successful, please wait patiently for the review"
              );
              this.$store.dispatch("getperons");
              this.$router.push("/my");
            } else {
              this.$toast(res.msg);
            }
            // window.location.href = res.url;
          })
          .catch((res) => {
            this.loading = false;
          });
      } else {
        this.loading = false;
        this.$toast("Please enter the amount");
      }
    },
    withdrawalHandle() {
      if (!this.iscrads) {
        this.$toast("Please add bank card");
      } else {
        this.paymengHandle();
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.fee {
  display: flex;
  align-items: center;
  padding: 10px 0;
  font-size: 13px;
  color: #333;
  span {
    font-size: 13px !important;
    margin: 0 0 0 7px;
  }
}
.loading {
  width: 30px;
  height: 30px;
  margin: auto;
  margin-top: 300px;
}
.with-card {
  box-sizing: border-box;
  padding: 18px 15px;
  display: flex;
  align-items: center;
  .icon {
    width: 35px;
    height: 35px;
    line-height: 35px;
    background: #4a67f5;
    border-radius: 50%;
    text-align: center;
  }
  .icon-Withdrawal2 {
    color: #fff;
  }
  .card-text {
    margin: 0 43px 0 10px;
    color: #333;
    font-size: 15px;
    p:first-child {
      color: #666666;
    }
    p:last-child {
      margin: 5px 0 0 0;
      color: #4a67f5;
    }
  }
  button {
    width: 120px;
    height: 35px;
    background: #d4d9f6;
    color: #4a67f5;
    font-size: 13px;
    border-radius: 23px;
    border: none;
  }
}
.withdrawal-content {
  height: 72vh;
  padding: 34px 15px;
  border-radius: 10px 10px 0 0;
  background-color: #fff;
  p {
    padding: 0 0 20px 0;
    font-size: 15px;
    color: #333;
  }
  span {
    font-size: 46px;
  }
  input {
    height: 46px;
    width: 280px;
    font-size: 46px;
    padding: 0 5px;
    color: #4a67f5;
    border: none;
  }
  .bd-line {
    height: 1px;
    background-color: #e2e2e2;
  }
}
.with-btn {
  padding: 30px 0;
  text-align: center;
  button {
    width: 300px;
    height: 50px;
    background: #4a67f5;
    opacity: 1;
    border-radius: 105px;
    border: none;
    color: #fff;
    font-size: 17px;
  }
}
</style>